﻿// Connect.cpp: 实现文件
//

#include "pch.h"
#include "LiWi.h"
#include "afxdialogex.h"
#include "Connect.h"
#include "liwishell.h"
#include "Data.h"


// Connect 对话框

IMPLEMENT_DYNAMIC(Connect, CDialogEx)

Connect::Connect(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_Connect, pParent)
{

}

Connect::~Connect()
{
	
}

void Connect::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(Connect, CDialogEx)

	ON_BN_CLICKED(IDC_IPConnect, &Connect::OnBnClickedconnect)
	ON_BN_CLICKED(IDC_RADIOIP, &Connect::OnBnClickedRadioip)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTONEnable0, &Connect::OnBnClickedButtonenable0)
	ON_BN_CLICKED(IDC_BUTTONEnable1, &Connect::OnBnClickedButtonenable1)
	ON_BN_CLICKED(IDC_BUTTONEnable2, &Connect::OnBnClickedButtonenable2)
	ON_BN_CLICKED(IDC_BUTTONEnable3, &Connect::OnBnClickedButtonenable3)
	ON_BN_CLICKED(IDC_BUTTONEnable4, &Connect::OnBnClickedButtonenable4)
END_MESSAGE_MAP()


// Connect 消息处理程序





/// <summary>
/// IP连接
/// </summary>
void Connect::OnBnClickedconnect()
{
	// TODO: 在此添加控件通知处理程序代码
	CWnd* pCtrl = GetDlgItem(IDC_IPConnect);
	if (Data::CardIsConnect())
	{
		PMC_Close(Data::handle);
		pCtrl->SetWindowTextW(_T("连接"));

		for (size_t i = 0; i < 6; i++)
		{
			PMC_SetAxisEnable(Data::handle, i, 0);			
		}
		Data::handle = nullptr;

	}
	else
	{
		int ret = 0;

		if (((CButton*)GetDlgItem(IDC_RADIOIP))->GetCheck()) {
			CString str;
			USES_CONVERSION;		//声明标识符
			GetDlgItem(IDC_EDIT1)->GetWindowTextW(str);
			char* ip = T2A(str);
			ret = PMC_OpenEth(ip, &Data::handle);
		}
		else
		{
			uint comid = GetDlgItemInt(IDC_EDIT2);		//串口号
			uint dwBaudRate = 115200;					//波特率 115200
			uint dwByteSize = 8;						//数据位 8位
			uint dwParity = 0;							//用来连接不同的控制器，0-TCP平台控制，1-RTU系统控制器
			uint dwStopBits = 0;						//停止位 0
			ret = PMC_SetComDefaultBaud(dwBaudRate, dwByteSize, dwParity, dwStopBits);
			if (0 != ret)
			{
				ret = PMC_OpenCom(comid, &Data::handle);
			}
		}

		if (0 != ret)
		{
			Data::handle = nullptr;
			MessageBox(TEXT("连接失败"));
		}
		else
		{
			pCtrl->SetWindowTextW(_T("断开"));
			//连接成功打开所有使能
			for (size_t i = 0; i < 6; i++)
			{
				PMC_SetAxisEnable(Data::handle, i, 1);
				PMC_SetAtype(Data::handle, i, 1);
				SetTimer(1, 200, NULL);
			}
		}

	}
}



void Connect::OnBnClickedRadioip()
{
	// TODO: 在此添加控件通知处理程序代码

}

/// <summary>
/// 初始化窗口
/// </summary>
/// <returns></returns>
BOOL Connect::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	((CButton*)GetDlgItem(IDC_RADIOIP))->SetCheck(true);
	GetDlgItem(IDC_EDIT1)->SetWindowTextW(TEXT("192.168.1.11"));	//IP文本框

	//SetTimer(1, 200, NULL);


	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}




//设置使能
//void Connect::SetAxisEnable(CWnd* PctrlEnable, int i);



void Connect::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	if (Data::CardIsConnect())
	{
		int value = 0;
		for (size_t i = 0; i < 5; i++)
		{
			PMC_GetAxisEnable(Data::handle, i, &value);

				switch (i)
				{
				case 0:
					GetDlgItem(IDC_BUTTONEnable0)->SetWindowTextW(value == 1 ? _T("AXIS0关使能") : _T("AXIS0使能"));
					break;
				case 1:
					GetDlgItem(IDC_BUTTONEnable1)->SetWindowTextW(value == 1 ? _T("AXIS1关使能") : _T("AXIS1使能"));
					break;
				case 2:
					GetDlgItem(IDC_BUTTONEnable2)->SetWindowTextW(value == 1 ? _T("AXIS2关使能") : _T("AXIS2使能"));
					break;
				case 3:
					GetDlgItem(IDC_BUTTONEnable3)->SetWindowTextW(value == 1 ? _T("AXIS3关使能") : _T("AXIS3使能"));
					break;
				case 4:
					GetDlgItem(IDC_BUTTONEnable4)->SetWindowTextW(value == 1 ? _T("AXIS4关使能") : _T("AXIS4使能"));
					break;
				default:
					break;
				}

		}
	}
	else
	{
		GetDlgItem(IDC_BUTTONEnable0)->SetWindowTextW(_T("AXIS0使能"));
		GetDlgItem(IDC_BUTTONEnable1)->SetWindowTextW(_T("AXIS1使能"));
		GetDlgItem(IDC_BUTTONEnable2)->SetWindowTextW(_T("AXIS2使能"));
		GetDlgItem(IDC_BUTTONEnable3)->SetWindowTextW(_T("AXIS3使能"));
		GetDlgItem(IDC_BUTTONEnable4)->SetWindowTextW(_T("AXIS4使能"));
		KillTimer(1);
	}


	CDialogEx::OnTimer(nIDEvent);
}


void Connect::OnBnClickedButtonenable0()
{
	// TODO: 在此添加控件通知处理程序代码
	CString value;
	GetDlgItem(IDC_BUTTONEnable0)->GetWindowTextW(value);
	if (value == "AXIS0使能")
		PMC_SetAxisEnable(Data::handle, 0, 1);
	else
		PMC_SetAxisEnable(Data::handle, 0, 0);

}


void Connect::OnBnClickedButtonenable1()
{
	// TODO: 在此添加控件通知处理程序代码
	CString value;
	GetDlgItem(IDC_BUTTONEnable1)->GetWindowTextW(value);
	if (value == "AXIS1使能")
		PMC_SetAxisEnable(Data::handle, 1, 1);
	else
		PMC_SetAxisEnable(Data::handle, 1, 0);
}


void Connect::OnBnClickedButtonenable2()
{
	// TODO: 在此添加控件通知处理程序代码
	CString value;
	GetDlgItem(IDC_BUTTONEnable2)->GetWindowTextW(value);
	if (value == "AXIS2使能")
		PMC_SetAxisEnable(Data::handle, 2, 1);
	else
		PMC_SetAxisEnable(Data::handle, 2, 0);
}


void Connect::OnBnClickedButtonenable3()
{
	// TODO: 在此添加控件通知处理程序代码
	CString value;
	GetDlgItem(IDC_BUTTONEnable3)->GetWindowTextW(value);
	if (value == "AXIS3使能")
		PMC_SetAxisEnable(Data::handle, 3, 1);
	else
		PMC_SetAxisEnable(Data::handle, 3, 0);
}


void Connect::OnBnClickedButtonenable4()
{
	// TODO: 在此添加控件通知处理程序代码
	CString value;
	GetDlgItem(IDC_BUTTONEnable4)->GetWindowTextW(value);
	if (value == "AXIS4使能")
		PMC_SetAxisEnable(Data::handle, 4, 1);
	else
		PMC_SetAxisEnable(Data::handle, 4, 0);
}
